package es.titoapps.spanishhandball.tools;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.widget.Toast;
import es.titoapps.spanishhandball.R;
import es.titoapps.spanishhandball.entities.New;

public class Utilities {

	
	public static void showExitConfirmationDialog(Context context,String title,String message,String positiveButton,String negativeButton,DialogInterface.OnClickListener listener){
		
		
		//Ask the user if they want to quit
        new AlertDialog.Builder(context)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(R.string.salirTitle)
        .setMessage(R.string.salirMessage)
        .setPositiveButton(R.string.aceptar, listener)
        .setNegativeButton(R.string.cancelar, null)
        .show();
        
     
	}
	
	public static String getCodificacion(String source){
		String codificacion;
		
		if(source.equalsIgnoreCase("ALAMADERA")||source.equalsIgnoreCase("MUNDOBALONMANO")||source.equalsIgnoreCase("MUCHOBALONMANOACTUALIDAD")||source.equalsIgnoreCase("MUCHOBALONMANONOTICIAS")||source.equalsIgnoreCase("DEROSCA"))
	    	codificacion="UTF-8";
	    else
	    	codificacion="ISO-8859-1";
		
		return codificacion;
		
	}
	public static int getSourceLogoImageResourceId(New currentNew){
		
		if(currentNew.getSource().equalsIgnoreCase("MARCA"))
			return R.drawable.ico_marca;
		else if(currentNew.getSource().equalsIgnoreCase("AS"))
			return R.drawable.ico_as;
		else if(currentNew.getSource().equalsIgnoreCase("MUNDODEPORTIVO"))
			return R.drawable.ico_mundodeportivo;
		else if(currentNew.getSource().equalsIgnoreCase("SPORT"))
			return R.drawable.ico_sport;
		else if(currentNew.getSource().equalsIgnoreCase("MUCHOBALONMANOACTUALIDAD")||
				currentNew.getSource().equalsIgnoreCase("MUCHOBALONMANONOTICIAS"))
			return R.drawable.ico_muchobalonmano;
		else if(currentNew.getSource().equalsIgnoreCase("DEROSCA"))
			return R.drawable.ico_cope;
		else if(currentNew.getSource().equalsIgnoreCase("ALAMADERA"))
			return R.drawable.ico_as;
		else 
			return R.drawable.ic_launcher;
	}
	
	public static String getSourceName(New currentNew,Context context){
		
		if(currentNew.getSource().equalsIgnoreCase("MARCA"))
			return context.getString(R.string.sourcesMarcaTitle);
		else if(currentNew.getSource().equalsIgnoreCase("AS"))
			return context.getString(R.string.sourcesASTitle);
		else if(currentNew.getSource().equalsIgnoreCase("MUNDODEPORTIVO"))
			return context.getString(R.string.sourcesMundoDeportivoTitle);
		else if(currentNew.getSource().equalsIgnoreCase("SPORT"))
			return context.getString(R.string.sourcesSportTitle);
		else if(currentNew.getSource().equalsIgnoreCase("MUCHOBALONMANOACTUALIDAD")||
				currentNew.getSource().equalsIgnoreCase("MUCHOBALONMANONOTICIAS"))
				return context.getString(R.string.sourcesMundoBalonmanoTitle);
		else if(currentNew.getSource().equalsIgnoreCase("DEROSCA"))
				return context.getString(R.string.sourcesDeRoscaTitle);
		else if(currentNew.getSource().equalsIgnoreCase("ALAMADERA"))
			return context.getString(R.string.sourcesALaMaderaTitle);
		else 
			return "";
	}
	
	public static void shareContent(New currentNew,Context context){
		
		String message = currentNew.getTitle()+" "+currentNew.getLink();
		Intent share = new Intent(Intent.ACTION_SEND);
		share.setType("text/plain");
		share.putExtra(Intent.EXTRA_TEXT, message);

		context.startActivity(Intent.createChooser(share, "Noticia"));
		
	}
	
	public static void shareApp(Context context){
		
//		String uri = "<a href=\""+context.getString(R.string.shareAppUri) + context.getString(R.string.app_package)+"\"";
//		
//		String message = "<html><header></header><body>"+context.getString(R.string.shareAppMessage)+uri+">"+context.getString(R.string.app_name)+"</a></body></html>";
//		
//		Intent share = new Intent(Intent.ACTION_SEND);
//		share.setType("text/html");
//		share.putExtra(Intent.EXTRA_SUBJECT,context.getString(R.string.shareAppTitle));
//		share.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(message));
//
//		context.startActivity(Intent.createChooser(share, context.getString(R.string.shareAppTitle)));
		
		String uri = context.getString(R.string.shareAppUri) + context.getString(R.string.app_package);
		
		String message =context.getString(R.string.shareAppMessage)+uri;
		
		Intent share = new Intent(Intent.ACTION_SEND);
		share.setType("text/plain");
		share.putExtra(Intent.EXTRA_SUBJECT,context.getString(R.string.shareAppTitle));
		share.putExtra(Intent.EXTRA_TEXT, message);

		context.startActivity(Intent.createChooser(share, context.getString(R.string.shareAppTitle)));
	}
	
	public static void launchContentOnBrowser(New currentNew,Context context){
		
		if(NetworkTools.internetAvailable(context)){
			Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(currentNew.getLink()));
			context.startActivity(browserIntent);
			
		}else
			Utilities.showToastConectionError(context);
		
	}
	
	public static void showToastConectionError(Context context){
		Toast.makeText(context, context.getString(R.string.mensaje_no_hay_conexion),Toast.LENGTH_SHORT).show();
	}
	
}